var SceneEditor = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.sceneId = options.sceneId;
	this.globe = options.editor.globe;
	this.isOpen = false;
	this.dIndex;
	this.scene;
	this.dottedBox;
	this.state=false;
	return this;
}

SceneEditor.prototype.init = function() {
	var _this = this;
}

SceneEditor.prototype.isState = function() {
	var _this = this;
	return _this.state;
}

SceneEditor.prototype.setState = function(state) {
	var _this = this;
	_this.state=state;
}

SceneEditor.prototype.edit = function(id) {
	let _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.closeAll();
	layer.load(2);
	var content = common_ajax.ajaxFunc('get', _this.ctx + "/scene/edit", { sceneId: _this.sceneId }, "html", null);
	layer.closeAll('loading');
	if (content) {
		layer.open({
			id: 'editScene',
			type: 1,
			anim: 5,
			title: '场景编辑',
			area: '300px',
			content: content,
			shade: 0,
			offset: 'l',
			fixed: true,
			move: false,
			skin: 'layui-layer  layui-layer-adminRight layui-layer-city',
			success: function(layero, dIndex) {
				_this.dIndex = dIndex;
				_this.isOpen=true;
				$(layero).children('.layui-layer-content').css('overflow', 'hidden');
				$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
				form.render();

				$('#modelBizScene-cover-btn').click(function() {
					let image=_this.globe.exportToImg(250.2,160.2);
					$('#modelBizScene-cover').attr("src", image);
					$('#modelBizScene-cover-input').val(image);
				});
				
				$('#modelBizScene-visualAngle-btn').click(function() {
					var v = _this.editor.VFG.Util.getVisualAngle(_this.editor.globe.viewer);
					if (v) {
						let image=_this.globe.exportToImg(250.2,160.2);
						$('#modelBizScene-visualAngle').attr("src", image);
						$('#modelBizScene-visualAngleImg-input').val(image);
						$('#scene-heading-input').val( v.heading);
						$('#scene-pitch-input').val(v.pitch);
						$('#scene-roll-input').val(v.roll);
						$('#scene-x-input').val(v.position.x);
						$('#scene-y-input').val(v.position.y);
						$('#scene-z-input').val(v.position.z);						
					}
				});

				form.on('submit(submitBaseBizSceneForm)', function(data) {
					layer.load(2);
					var formData = {};
					for (var key in data.field) {
						formData['scene.' + key] = data.field[key];
					}
					var res = common_ajax.ajaxFunc('post', _this.ctx + "/scene/save", formData, "json", null);
					layer.closeAll('loading');
					if (200 == res.code) {
						layer.msg(res.message, {icon: 1});
						layer.close(_this.dIndex);
						_this.isOpen=false;
						_this.editor.scene=res.data;
						$("#scene-name-span").html(res.data.name);
					} else {
						layer.msg(res?res.message:'异常！', {icon: 2});
					}
					return false;
				});
			},
			end: function() { 
				_this.isOpen=false;
			}
		});
	}else{
		layer.msg(res?res.message:'异常！', {icon: 2});
	}
}

SceneEditor.prototype.myScene = function() {
	let _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/list", {}, "html", null);
	if (res != undefined && res) {
		layer.closeAll();
		layer.open({
			id: 'myScene',
			type: 1,
			anim: 5,
			title: '场景列表',
			area: '282px',
			content: res,
			shade: 0,
			offset: 'l',
			fixed: true,
			move: false,
			skin: 'layui-layer  layui-layer-adminRight layui-layer-city',
			success: function(layero, dIndex) {
				_this.isOpen=true;
				_this.dIndex = dIndex;
				$(layero).children('.layui-layer-content').css('overflow', 'hidden');
				$(layero).children('.layui-layer-content').css('height', 'calc(100% - 31px)');
				form.render();
				_this.loadSceneList(1);
			},
			end: function() { 
				_this.isOpen=false;
			}
		});
	}
}

SceneEditor.prototype.loadSceneList = function(curr) {
	var _this = this;
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/listData", { page: curr ? curr : 1,limit: 5}, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(selectScenesScript.innerHTML).render(res, function(html) {
			$('#select-scene-list').html(html);
			$(".ImageList>.content>.item>.IconButton.scene-delete").on('click', function() {
				_this.delete($(this).attr('id'));
			})
			$(".ImageList>.content>.item>.IconButton.scene-edit").on('click', function() {
				_this.isOpen=false;
				layer.close(_this.dIndex);
				_this.editScene($(this).attr('id'));
			})
			$(".ImageList>.content>.item>.Thumbnail").on('click', function() {
				_this.switch($(this).attr('id'));
			})
		});
		laypage.render({
			elem: 'tableBizScenePageBar',
			count: res.count,
			curr: curr,
			limit: 5,
			layout: ['prev', 'next', 'count'],
			jump: function(obj, first) {
				if (!first) {
					_this.loadSceneList(obj.curr);
				}
			}
		});
	}
}

SceneEditor.prototype.delete=function(id,name){
	var _this=this;
    layer.confirm('确定要删除场景吗？', {
        shade: .1
    }, function (index) {
        layer.close(index);
        layer.load(2);
		var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/delete", {
			id:id
		}, "json", null);
		layer.closeAll('loading');
        if (res && 200==res.code) {
            layer.msg(res.message, {icon: 1});
			_this.loadSceneList(1);
            return true;
        } else {
            layer.msg(res.message, {icon: 2});
            return false;
        }
    }, function (index) {
        layer.close(index);
        return false;
    }); 
}

/**
 * 切换场景
 */
SceneEditor.prototype.switch = function(id) {
	var _this=this;
	layer.confirm('确定要切换至该场景吗？', {
	    shade: .1
	}, function (index) {
		if(_this.editor.scene && _this.editor.scene.id==id){
	        layer.msg("已处于该场景中。", {icon: 3});
	        return false;
		}else{
		    layer.close(index);
			layer.load(2);
			var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/getById", {id:id}, "json", null);
			layer.closeAll('loading');
		    if (res && 200==res.code) {
		    	layer.closeAll();
		    	_this.clear();
				_this.editor.scene=res.data;
				_this.editor.sceneHeader.showTool();
				_this.flyToHome();
				_this.load();
		        return true;
		    } else {
		        layer.msg(res.message, {icon: 2});
		        return false;
		    }
		}
	}, function (index) {
	    layer.close(index);
	    return false;
	}); 
}

SceneEditor.prototype.flyToHome=function(){
	let _this=this;
	if(_this.editor.scene.x && _this.editor.scene.y && _this.editor.scene.z){
		_this.globe.flyTo({
			position:{
				x:_this.editor.scene.x*1,
				y:_this.editor.scene.y*1,
				z:_this.editor.scene.z*1,
			},
			orientation:{
				heading:_this.editor.scene.heading*1,
				pitch:_this.editor.scene.pitch*1,
				roll:_this.editor.scene.roll*1,
			}
		});
	}
}

SceneEditor.prototype.openDottedBox=function(){
	var _this = this;
	if(!_this.dottedBox){
		_this.dottedBox = layer.open({
			type: 1,
			title: false,
			closeBtn: false,
			shade: 0,
			btnAlign: 'c',
			moveType: 1,
			area: ['834px', '534px'],
			content: '<div class="dottedBox"><span class="dottedBox-border row1"></span><span class="dottedBox-border row2"></span><span class="dottedBox-border col1"></span><span class="dottedBox-border col2"></span></div>',
			skin: 'layui-layer  layui-layer-outarea',
			success: function(layero) {
				$(layero).children('.layui-layer-content').css('overflow', 'hidden');
				$(layero).children('.layui-layer-content').css('height', '100%');
			}
		});
	}
}

SceneEditor.prototype.closeDottedBox=function(){
	var _this = this;
	if(_this.dottedBox){
		layer.close(_this.dottedBox);
		delete _this.dottedBox;
	}
}

SceneEditor.prototype.clear=function(){
	let _this=this;
	_this.globe.clearAll();
	_this.editor.sceneAnimation.clearAll();
}


SceneEditor.prototype.load=function(){
	let _this=this;
	let res =common_ajax.ajaxFunc('get', _this.ctx + "/scene/load",{
		'sceneId':this.editor.scene? this.editor.scene.id:'',
	}, "json", null);
	if(200==res.code){
		for(let obj of res.data){
			_this.globe.render(_this.editor.sceneUtil.package(obj));
		}
	}
}


